<?php
/**
 * @action 消费统计控制器
 * @authors guo (guomingliangphp@163.com)
 * @date    2014-01-13 18:20:18
 * @version $Id$
 */

class ConsumptionAction extends CommonAction {

    //记录代理商的每天消费记录
    public function index(){

        $proxyname = $_SESSION['agent']['username'];
        $proxyid = $_SESSION['agent']['id'];
        $condition['proxyname'] = $proxyname;

        $names=R("Agent/userlist");
        $this->names=$names;

        $money = $this->agent_consumption();
        $this->money = $money;

        if(IS_GET){

            if(I("get.userName")){
                $username = I("get.userName");
                $website = I("get.site");
                $result = M('user')->where("ProxyId = $proxyid and Username = '".$username."'")->find();

                if(!$result){
                    $this->error("您没有此用户!");
                }
                $condition['username'] = $username;
                $condition['website'] = $website;
            }

            if(I("get.stime")&&I("get.etime")){
                $stime=I("get.stime");
                $etime=I("get.etime");
                $etime=date('Y-m-d',strtotime('+1 day',strtotime($etime)));
                $condition['addtime']=array('between',array($stime,$etime));
            }            
        }
        import("ORG.Util.Page");
        $pagenumber=10;
        $consumelist = M('consume')->page($_GET['p'])->where($condition)->order('id desc')->limit($pagenumber)->select();
        $this->assign('list',$consumelist);

        $totalnum= count(M('consume')->where($condition)->select());
        $page=new Page($totalnum,$pagenumber);
        $page->setConfig("prev","上一页");
        $page->setConfig("next","下一页");
        $page->setConfig("first","首页");
        $page->setConfig("last","末页");
        $page->setConfig("theme","<span class=pageinfo> <u></u>当前 %nowPage%/%totalPage%页</span>
                                     %first% %upPage%   %linkPage%  %downPage% %end%");
        $pageshow=$page->show();
        $this->assign("pageshow",$pageshow);
        $this->display();
    }

    //代理商消费统计
    public function agent_consumption(){
        $consumeModel = M('consume');

        $proxyname = $_SESSION['agent']['username'];
        //累计消费
        $money['total'] = $consumeModel->field("SUM(money) as totalmonth")
        ->where("proxyname = '".$proxyname."'")
        ->select();
        $money['total'] = $money['total'][0]['totalmonth'];

        //平均消费
        $sql="select to_days(now())-to_days(min(addtime)) as days from yst_consume";
        $days=$consumeModel->query($sql);
        $days=$days[0]['days'];
        if($days<7){
            $sql="select sum(money) tmoney from yst_consume where proxyname='".$proxyname."'";
            $tmoney=$consumeModel->query($sql);
            $tmoney=$tmoney[0]['tmoney'];
            if($days==0){
                $money['avg']=$tmoney;
            }else{
                $money['avg']=$tmoney/$days;
                $money['avg'] = sprintf("%.1f",$money['avg']);
            }
        }else{
            $sql = 'SELECT SUM(money) as tmoney';
            $sql.= ' FROM yst_consume ';
            $sql.= " WHERE proxyname='".$proxyname."'";
            $sql.= " AND DATE_FORMAT(addtime, '%Y-%m-%d' ) > DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 7 day),'%Y-%m-%d')";
            $totalMoney=$consumeModel->query($sql);
            $totalMoney=$totalMoney[0]['tmoney'];
            $money['avg']=$totalMoney/7;
            $money['avg'] = sprintf("%.1f",$money['avg']);
        }
        //上个月的消费统计
        $money['lastmonth'] = $consumeModel->field("SUM(money) as lastmonth")
        ->where("DATE_FORMAT(addtime, '%Y-%m' ) = DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 MONTH),'%Y-%m') and proxyname='".$proxyname."'")
        ->select();
        $money['lastmonth'] = $money['lastmonth'][0]['lastmonth'];

        //本月消费统计
        $money['monthmoney'] = $consumeModel->field("SUM(money) as monthmoney")
        ->where("DATE_FORMAT(addtime, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) and proxyname='".$proxyname."'")
        ->select();
        $money['monthmoney'] = $money['monthmoney'][0]['monthmoney'];

        //昨日消费
        $money['yesterday'] = $consumeModel->field("SUM(money) as yesterdaymoney")
        ->where("TO_DAYS( NOW( ) ) - TO_DAYS(addtime) = 1 and proxyname='".$proxyname."'")
        ->select();
        $money['yesterday'] = $money['yesterday'][0]['yesterdaymoney'];

        return $money;
    }
    
   //指定代理商下的所有客户的消费列表(包括对消费列表的查询)
    Public function userconsumptionalllist(){

        //代理商下的用户消费统计
        $names=R("Agent/userlist");
        $this->names=$names;

        $money=R("Agent/user_consumption");
        $this->money=$money;


        Load('extend');
        $consumptionModel=M("consumption");
        $agentId=$_SESSION['agent'][id];
        $condition["username"]=array('exp',"in($names)");

        //对消费列表的查询条件
        if(I("get.userName")){
            $userName=I("get.userName");
            $condition['username']=array(array('exp',"in($names)"),array('eq',$userName));
            if(I("get.site")){
               $site=I("get.site");
                $condition['website']=$site;
            }
            if(I("get.stime")&&I("get.etime")){
                $stime=I("get.stime");
                $etime=I("get.etime");
                $etime=date('Y-m-d',strtotime('+1 day',strtotime($etime)));
                $condition['addtime']=array('between',array($stime,$etime));
            }
        }
        import("ORG.Util.Page");
        $pagenumber=10;
        $list=$consumptionModel->page($_GET['p'])->where($condition)->order('id desc')->limit($pagenumber)->select();
        $totalnum= count($consumptionModel->where($condition)->select());
        $this->assign("list",$list);
        $page=new Page($totalnum,$pagenumber);
        $page->setConfig("prev","上一页");
        $page->setConfig("next","下一页");
        $page->setConfig("first","首页");
        $page->setConfig("last","末页");
        $page->setConfig("theme","<span class=pageinfo> <u></u>当前 %nowPage%/%totalPage%页</span>
                                     %first% %upPage%   %linkPage%  %downPage% %end%");
        $pageshow=$page->show();
        $this->assign("pageshow",$pageshow);
        $this->display();
    }

    //某一用户的消费统计
    public function userconsumptionlist(){

        Load('extend');
        $agentId=$_SESSION['agent']['id'];
        $userId=I("get.userId");
        $condition['ProxyId']=$agentId;
        $condition['Id']=$userId;
        $userlist=M('user')->where($condition)->find();
        $userName=$userlist['Username'];
        if(!$userlist) $this->error("用户不存在");

        $this->userInfo=$userlist;
        $user=new UserAction();
        $money=$user-> usertongji($userId);
        $this->assign('money',$money);

        import("ORG.Util.Page");
        $pagenumber=10;
        $list=M('consumption')->page($_GET['p'])->where("username='".$userName."'")->order('id desc')->limit($pagenumber)->select();
        $totalnum= count(M('consumption')->where("username='".$userName."'")->select());
        $this->assign("list",$list);
        $page=new Page($totalnum,$pagenumber);
        $page->setConfig("prev","上一页");
        $page->setConfig("next","下一页");
        $page->setConfig("first","首页");
        $page->setConfig("last","末页");
        $page->setConfig("theme","<span class=pageinfo> <u></u>当前 %nowPage%/%totalPage%页</span>
                                     %first% %upPage%   %linkPage%  %downPage% %end%");
        $pageshow=$page->show();
        $this->assign("pageshow",$pageshow);
        $this->display();
    }
}